﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System.Windows.Input;
using Bio.Views.Structure.Editor.Internal.Rendering;
using Bio.Views.Structure.Editor.Internal.Tools.Interfaces;
using JulMar.Windows.Mvvm;

namespace Bio.Views.Structure.Editor.Internal.Tools
{
    public abstract class SecondaryStructureDrawingTool : ViewModel, IStructureDrawingTool
    {
        public SecondaryStructureDrawing Drawing
        {
            get;
            set;
        }

        //This will change to an image at some point.
        private string _icon;
        public string Icon
        {
            get { return _icon; }
            protected set
            {
                _icon = value;
                OnPropertyChanged("Icon");
            }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }

        private string _toolTip;
        public string ToolTip
        {
            get { return _toolTip; }
            set
            {
                _toolTip = value;
                OnPropertyChanged("ToolTip");
            }
        }

        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (!IsEnabled)
                    _isActive = false;
                else
                    _isActive = value;

                if (_isActive)
                    HookInputEvents();
                else
                    UnhookInputEvents();

                OnActiveStateChanged();
                OnPropertyChanged("IsActive");
            }
        }

        private bool _isEnabled;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }

        public virtual void OnSelectionModeChanged()
        {
        }

        protected virtual void OnActiveStateChanged()
        {
        }

        protected virtual void DrawingPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
        { 
        }

        protected virtual void DrawingPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        protected virtual void DrawingPreviewMouseMove(object sender, MouseEventArgs e)
        {
        }

        protected virtual void DrawingMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }

        protected virtual void DrawingMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        protected virtual void DrawingMouseLeave(object sender, MouseEventArgs e)
        {
        }

        protected virtual void DrawingMouseMove(object sender, MouseEventArgs e)
        {
        }

        private void HookInputEvents()
        {
            if (Drawing != null)
            {
                Drawing.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(DrawingPreviewMouseLeftButtonDown);
                Drawing.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(DrawingPreviewMouseLeftButtonUp);
                Drawing.PreviewMouseMove += new MouseEventHandler(DrawingPreviewMouseMove);
                Drawing.MouseLeftButtonDown += new MouseButtonEventHandler(DrawingMouseLeftButtonDown);
                Drawing.MouseLeftButtonUp += new MouseButtonEventHandler(DrawingMouseLeftButtonUp);
                Drawing.MouseLeave += new MouseEventHandler(DrawingMouseLeave);
                Drawing.MouseMove += new MouseEventHandler(DrawingMouseMove);
            }
        }

        private void UnhookInputEvents()
        {
            if (Drawing != null)
            {
                Drawing.PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(DrawingPreviewMouseLeftButtonDown);
                Drawing.PreviewMouseLeftButtonUp -= new MouseButtonEventHandler(DrawingPreviewMouseLeftButtonUp);
                Drawing.MouseLeftButtonDown -= new MouseButtonEventHandler(DrawingMouseLeftButtonDown);
                Drawing.MouseLeftButtonUp -= new MouseButtonEventHandler(DrawingMouseLeftButtonUp);
                Drawing.MouseLeave -= new MouseEventHandler(DrawingMouseLeave);
                Drawing.MouseMove -= new MouseEventHandler(DrawingMouseMove);
                Drawing.PreviewMouseMove -= new MouseEventHandler(DrawingPreviewMouseMove);
            }
        }
    }
}
